<template>
  <div>
    <input type="file" @change="onFileChange" accept=".doc,.docx" />
    <button @click="uploadFile">上传文件</button>
    <!-- <button @click="downloadFile">下载文件</button> -->
  </div>
</template>
  <script>
import axios from "axios";

export default {
  data() {
    return {
      selectedFile: null,
    };
  },
  methods: {
    downloadFile() {
      axios({
        url: "/download/work.docx", // 替换为实际文件名
        method: "GET",
        responseType: "blob", // 以二进制流形式接收文件
      }).then((response) => {
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", "work.docx"); // 设置下载文件名
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    },
    onFileChange(event) {
      this.selectedFile = event.target.files[0];
    },
    async uploadFile() {
      if (this.selectedFile) {
        const formData = new FormData();
        formData.append("file", this.selectedFile);
        try {
          const response = await axios.post("/upload", formData, {
            headers: {
              "Content-Type": "multipart/form-data",
            },
          });
        } catch (error) {
          console.error("文件上传失败", error);
        }
      } else {
        alert("请选择文件");
      }
    },
  },
};
</script>